package com.syxp.pda.action.detail;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.bean.FieldBean;
import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.util.Operation;
import com.syxp.pda.core.util.TimeFormatUtil;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.ClassDuty;
import com.syxp.pda.entity.base.DefineIDZone;
import com.syxp.pda.entity.base.Department;
import com.syxp.pda.entity.base.Duty;
import com.syxp.pda.entity.detail.CheckPlanDetail;
import com.syxp.pda.entity.detail.CheckPlanDetailDTO;
import com.syxp.pda.service.base.DepartmentService;
import com.syxp.pda.service.detail.PlanDetailService;
import com.syxp.pda.service.plan.CHKLineService;

public class PlanDetailAction extends BaseAction{

	@Resource
	private CHKLineService chklineService ;
	
	@Resource
	private DepartmentService departmentService ;
	
	@Resource
	private PlanDetailService planDetailService ;
	
	private CheckPlanDetailDTO planDetail ;
	
	public CheckPlanDetailDTO getPlanDetail() {
		return planDetail;
	}

	public void setPlanDetail(CheckPlanDetailDTO planDetail) {
		this.planDetail = planDetail;
	}

	private int departmentID ;
	
	public int getDepartmentID() {
		return departmentID;
	}

	public void setDepartmentID(int departmentID) {
		this.departmentID = departmentID;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public CHKLineService getChklineService() {
		return chklineService;
	}

	public void setChklineService(CHKLineService chklineService) {
		this.chklineService = chklineService;
	}

	public PlanDetailService getPlanDetailService() {
		return planDetailService;
	}

	public void setPlanDetailService(PlanDetailService planDetailService) {
		this.planDetailService = planDetailService;
	}

	public String loadLineZone() {
		try{
			JsonObject jsonO = new JsonObject() ;
			if(planDetail.getChkLineID()!=0){
				Set<DefineIDZone> zones = chklineService.getLineZones(planDetail.getChkLineID()) ;
				JsonArray jsonA = new JsonArray() ;
				for(DefineIDZone zone : zones) {
					JsonObject jobj = new JsonObject() ;
					jobj.addProperty("zoneID", zone.getZoneID()) ;
					jobj.addProperty("zoneName", zone.getZoneName()) ;
					jsonA.add(jobj) ;
				}
				jsonO.add("zones", jsonA) ;
			}
			jsonO.addProperty("success", true) ;
			this.jsonString = gson.toJson(jsonO) ;
			System.out.println(this.jsonString) ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String loadDutyClass() {
		try{
			System.out.println(departmentID);
			Department department = departmentService.loadDutyClass(departmentID) ;
			JsonArray classes = new JsonArray() ;
			for(ClassDuty cla : department.getClassDutys()) {
				JsonObject jsonCla = new JsonObject() ;
				jsonCla.addProperty("classID", cla.getClassID()) ;
				jsonCla.addProperty("claName", cla.getClaName()) ;
				classes.add(jsonCla) ;
				
			}
			JsonArray dutys = new JsonArray() ;
			for(Duty duty : department.getDutys()) {
				JsonObject jsonDuty = new JsonObject() ;
				jsonDuty.addProperty("dutyID", duty.getDutyID()) ;
				jsonDuty.addProperty("dutyName", duty.getDutyName()) ;
				dutys.add(jsonDuty) ;
			}
			
			JsonObject jsonObj = new JsonObject() ;
			jsonObj.add("classes", classes) ;
			jsonObj.add("dutys", dutys) ;
			jsonObj.addProperty("success", true) ;
			this.jsonString = gson.toJson(jsonObj) ;
			System.out.println(jsonString) ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
			
		}
		return SUCCESS ;
	}
	
	public String query() {
		try{
			QueryFilter qf = new QueryFilter() ;
			List<CheckPlanDetail> planDetails = planDetailService.selectByQueryFilter(buildFilter(qf,planDetail)) ;
			JsonObject jsonObj = new JsonObject() ;
			jsonObj.add("details", gson.toJsonTree(planDetails));
			jsonObj.addProperty("success", true) ;
			this.jsonString = gson.toJson(jsonObj) ;
		}catch(Exception e){
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	private QueryFilter buildFilter(QueryFilter qf ,CheckPlanDetailDTO o) throws IllegalArgumentException, IllegalAccessException, ParseException {
		Field[] fields = o.getClass().getDeclaredFields() ;
		Field.setAccessible(fields, true) ;
		for(Field field : fields) {
			System.out.println(field.getName()) ;
			System.out.println(field.getType().getName()) ;
				if(field.getType().getName().equals("java.lang.String")&&!((String)field.get(o)).equals("")){
					if(field.getName().equals("startTime")){
						qf.addFilter(new FieldBean("pda_CHKDatetime" ,TimeFormatUtil.parseDateF((String)field.get(o)),Operation.GE)) ;
					}else if(field.getName().equals("endTime")) {
						qf.addFilter(new FieldBean("pda_CHKDatetime" ,TimeFormatUtil.parseDateF((String)field.get(o)),Operation.LE)) ;
					}else{
						qf.addFilter(new FieldBean(field.getName() ,field.get(o),Operation.LIKE)) ;
					}
				}else if(field.getType().getName().equals("int")&&field.getInt(o)!=0){
					qf.addFilter(new FieldBean(field.getName() ,field.get(o),Operation.EQUAL)) ;
				}
		}
		return qf ;
	}
}
